Imports System.Text
Imports System.Data
Imports System.Windows.Forms
Imports System.Data.SqlClient
Imports DevComponents.DotNetBar
Imports quanlydulich.Helpers

Partial Public Class FrmKetNoiCSDL
    Inherits Office2007Form

    Public Event Handler(ByVal sender As Object, ByVal e As MyConnectionEvent)
    Private pressedOK As Boolean = False

    Public Sub New()
        InitializeComponent()
    End Sub

    Private Sub frmKetNoiCSDL_Load(ByVal sender As Object, ByVal e As EventArgs)
        cmb.SelectedIndex = 0
    End Sub

    Private Sub btnCancel_Click(ByVal sender As Object, ByVal e As EventArgs)
        Application.Exit()
    End Sub

    Private Sub btnOK_Click(ByVal sender As Object, ByVal e As EventArgs)
        Me.Close()
        pressedOK = True
        Dim connEvent = New MyConnectionEvent()
        connEvent.DataSource = txtServer.Text
        connEvent.DataBaseName = cmbDatabase.Text
        connEvent.UserName = txtUsername.Text
        connEvent.Password = txtPassword.Text
        connEvent.ConnStatus = If(cmb.SelectedIndex = 0, "true", "false")
        RaiseEvent Handler(Me, connEvent)
    End Sub

    Private Sub btnTestConnection_Click(ByVal sender As Object, ByVal e As EventArgs)
        'Quyền Windows
        If cmb.SelectedIndex = 0 Then
            cmbDatabase.Items.Clear()
            Dim m_Conn As New SqlConnection("Data Source=" & txtServer.Text & ";Initial Catalog=master;Integrated Security=True;")
            Dim m_Cmd As New SqlCommand("SP_DATABASES", m_Conn)
            Dim m_DReader As SqlDataReader

            Try
                m_Conn.Open()
                m_DReader = m_Cmd.ExecuteReader()
                While m_DReader.Read()
                    cmbDatabase.Items.Add(m_DReader(0).ToString())
                End While
                MessageBoxEx.Show("Kết nối thành công!", "SUCCESSED", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch sqlEx As SqlException
                MessageBoxEx.Show(sqlEx.Message, "FAILED", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Finally
                If m_Conn.State = ConnectionState.Open Then
                    m_Conn.Close()
                End If

                m_Conn.Dispose()
                m_Cmd.Dispose()
            End Try
            txtUsername.Enabled = False
            txtPassword.Enabled = False
        End If

        'Quyền SQL Server
        If cmb.SelectedIndex = 1 Then
            cmbDatabase.Items.Clear()
            Dim m_Conn As New SqlConnection("Data Source=" & txtServer.Text & ";Initial Catalog=master;User Id=" & txtUsername.Text & ";Password=" & txtPassword.Text & ";")
            Dim m_Cmd As New SqlCommand("SP_DATABASES", m_Conn)
            Dim m_DReader As SqlDataReader

            Try
                m_Conn.Open()
                m_DReader = m_Cmd.ExecuteReader()
                While m_DReader.Read()
                    cmbDatabase.Items.Add(m_DReader(0).ToString())
                End While
                MessageBoxEx.Show("Kết nối thành công!", "SUCCESSED", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch sqlEx As SqlException
                MessageBoxEx.Show(sqlEx.Message, "SUCCESSED", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Finally
                If m_Conn.State = ConnectionState.Open Then
                    m_Conn.Close()
                End If

                m_Conn.Dispose()
                m_Cmd.Dispose()
            End Try
        End If
    End Sub

    Private Sub cmb_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        If cmb.SelectedIndex = 0 Then
            txtUsername.Enabled = False
            txtPassword.Enabled = False
        Else
            txtUsername.Enabled = True
            txtPassword.Enabled = True
        End If
    End Sub

    Private Sub FrmKetNoiCSDL_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
        If Not pressedOK Then
            Application.[Exit]()
        End If
    End Sub
End Class
